package wine.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import wine.ejb.utilities.Log;

public class ImportTag extends TagSupport {
	/**
	 * 页面js/css文件引入类
	 */
	private static final long serialVersionUID = 1L;

	public static final String LIBRARY_TYPE_EASYUI = "easyui";
	
	private String library;
	private String root;

	public String getRoot() {
		return (root != null ? root : ((HttpServletRequest)(this.pageContext.getRequest())).getContextPath() + "/");
	}

	public void setRoot(String root) {
		this.root = root;
	}

	public String getLibrary() {
		return library;
	}

	public void setLibrary(String library) {
		this.library = library;
	}
        public String _import(String type, String path, String className) {
		String result = "";

		if (path != null)
			path = this.getRoot() + path;

		if ("js".equalsIgnoreCase(type))
			result = "<script type='text/javascript' language='JavaScript' src='" + path + "' charset='UTF-8'></script>";
		else if ("css".equalsIgnoreCase(type))
			result = "<link type='text/css' rel='stylesheet' href='" + path + "' charset='UTF-8'/>";
		
		return result;
	}
        public int doEndTag() throws JspException {
		StringBuffer sbuffer = new StringBuffer("");
		sbuffer.append("");
		String[] libs = library.split(",");

		for (String lib : libs) {
			if(LIBRARY_TYPE_EASYUI.equalsIgnoreCase(lib)) {
				
				sbuffer.append(this._import("css", "css/ac.css", ""));
				sbuffer.append(this._import("css", "css/themes/default/easyui.css", ""));
				sbuffer.append(this._import("css", "css/themes/icon.css", ""));
				sbuffer.append(this._import("js", "js/jquery-1.8.0.min.js", ""));
				sbuffer.append(this._import("js", "js/jquery.easyui.min.js", ""));
				sbuffer.append(this._import("js", "js/easyui-lang-zh_CN.js", ""));
			}
		}

		try {
			String result = sbuffer.toString();
			pageContext.getOut().println(result);

		} catch (Exception e) {
			Log.error("xxx");
		}
		return super.doEndTag();
	}
	public void release() {
		super.release();
		this.library = null;

	}
}